package com.itheima.publisher.config;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * @version 1.0
 * @Author lwx
 * @Date 2024/12/27 18:01
 */
@Slf4j
@AllArgsConstructor
@Configuration
public class MQConfig {

    private final RabbitTemplate rabbitTemplate;

    @PostConstruct
    public void init() {
        rabbitTemplate.setReturnsCallback(new RabbitTemplate.ReturnsCallback() {
            @Override
            public void returnedMessage(ReturnedMessage returned) {
                log.error("触发return callback,");
                log.debug("exchange: {}", returned.getExchange());
                log.debug("routingKey: {}", returned.getRoutingKey());
                log.debug("message: {}", returned.getMessage());
                log.debug("replyCode: {}", returned.getReplyCode());
                log.debug("replyText: {}", returned.getReplyText());
            }
        });
    }

//    @Bean
//    public Exchange exchange(){
//        return ExchangeBuilder
//                .directExchange("demo.topic")
//                .durable(false)
//                .build();
//    }
//
//    @Bean
//    public Queue queue1(){
//        return QueueBuilder.nonDurable("q1").build();
//    }
//
//    @Bean
//    public Queue queue2(){
//        return QueueBuilder.nonDurable("q2").build();
//    }
//
//    @Bean
//    public Binding binding1(Exchange exchange,@Autowired @Qualifier("queue1") Queue queue1){
//        return BindingBuilder
//                .bind(queue1)
//                .to(exchange)
//                .with("a.#")
//                .noargs();
//    }
//
//    @Bean
//    public Binding binding2(Exchange exchange,@Autowired @Qualifier("queue2") Queue queue2){
//        return BindingBuilder
//                .bind(queue2)
//                .to(exchange)
//                .with("*.*")
//                .noargs();
//    }
}
